<% content_for :title, "Jobs" -%>

<div class='container'>
  <div class='row'>
    <div class='col-md-12'>
      <div class="page-header">
        <h2>
          Background Jobs
        </h2>
      </div>

      <div class='table-responsive'>
        <table class='table table-striped events'>
          <tr>
            <th>Status</th>
            <th>Agent / Job</th>
            <th>Created</th>
            <th>Next Run</th>
            <th>Attempts</th>
            <th>Last Error</th>
            <th></th>
          </tr>

        <% @jobs.each do |job| %>
          <tr>
            <td><%= status(job) %></td>
            <td><% case agent = agent_from_job(job)
                   when Agent
                   %><%= link_to(agent.name, agent_path(agent)) %><%
                   when String
                   %><%= agent %><%
                   when false
                   %>(system)<%
                   when nil
                   %>(deleted)<%
                   else
                   %>(unknown)<%
                   end %></td>
            <td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago<% if user = agent.try(:user) %> for <%= user.username %><% end %></td>
            <td title='<%= job.run_at %>'>
              <% if !job.failed_at %>
                <%= relative_distance_of_time_in_words job.run_at %>
              <% end %>
            </td>
            <td><%= job.attempts %></td>
            <td>
              <a data-toggle="modal" data-target="#error<%= job.id %>"><%= truncate job.last_error, :length => 90, :omission => "", :separator => "\n" %></a>
              <div class="modal fade" id="error<%= job.id %>" tabindex="-1" role="dialog" aria-labelledby="#<%= "error#{job.id}" %>" aria-hidden="true">
                <div class="modal-dialog big-modal-dialog">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                      <h4 class="modal-title" id="myModalLabel">Error Backtrace</h4>
                    </div>
                    <div class="modal-body">
                      <pre>
                        <%= raw html_escape(job.last_error).split("\n").join('<br/>') %>
                      </pre>
                    </div>
                  </div>
                </div>
              </div>
            </td>
            <td>
              <% if (!job.locked_at && !job.locked_by) || job.failed_at.present? %>
                <div class="btn-group btn-group-xs" style="float: right">
                  <% if (job.run_at > Time.now) || job.failed_at.present? %>
                    <%= link_to 'Run now', run_job_path(job), class: "btn btn-default", method: :put %>
                  <% end %>
                  <%= link_to 'Delete', job_path(job), class: "btn btn-danger", method: :delete, data: { confirm: 'Really delete this job?' } %>
                </div>
              <% end %>
            </td>
          </tr>
        <% end %>
        </table>
      </div>

      <%= paginate @jobs, :theme => 'twitter-bootstrap-3' %>

      <br />
      <div class="btn-group">
        <%= link_to destroy_failed_jobs_path, class: "btn btn-default", method: :delete do %>
          <span class="glyphicon glyphicon-trash"></span> Remove failed jobs
        <% end %>

        <%= link_to retry_queued_jobs_path, class: "btn btn-default", method: :post do %>
          <span class="glyphicon glyphicon-refresh"></span> Retry queued jobs
        <% end %>

        <%= link_to destroy_all_jobs_path, class: "btn btn-default", method: :delete, data: { confirm: "Are you sure you want to delete ALL pending jobs for all Huginn users?" } do %>
          <span class="glyphicon glyphicon-remove"></span> Remove all jobs
        <% end %>
      </div>
    </div>
  </div>
</div>

